package cn.pro.entity;

import javax.persistence.Column;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ClassUtils {
    public ClassUtils() {
    }

    public static Field getField(Class clazz, String fName) {
        try {
            return clazz.getDeclaredField(fName);
        } catch (NoSuchFieldException var3) {
            return clazz.getSuperclass() != Object.class ? getField(clazz.getSuperclass(), fName) : null;
        }
    }

    public static List<Field> getAllField(Class clazz) throws IntrospectionException {
        List<Field> result = new ArrayList();
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
        PropertyDescriptor[] ps = beanInfo.getPropertyDescriptors();
        Field f = null;
        PropertyDescriptor[] var4 = ps;
        int var5 = ps.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            PropertyDescriptor prop = var4[var6];
            f = getField(clazz, prop.getName());
            if (f != null) {
                result.add(f);
            }
        }

        return result;
    }

    public static String getProperty(Class clazz, String column) throws IntrospectionException {
        Optional<Field> optional = getAllField(clazz).stream().filter((field) -> {
            Column c = (Column)field.getAnnotation(Column.class);
            if (c == null) {
                return false;
            } else {
                return c.name().equalsIgnoreCase(column);
            }
        }).findFirst();
        return optional.isPresent() ? ((Field)optional.get()).getName() : "";
    }
}
